package com.axjy.scheduler.service.impl;

import com.axjy.comm.pojo.TpSchedulerSeat;
import com.axjy.comm.pojo.TpSchedulerSeatPrice;
import com.axjy.scheduler.mapper.SchedulerSeatPriceMapper;
import com.axjy.scheduler.mapper.SchedulerSeatMapper;
import com.axjy.scheduler.service.SchedulerSeatService;
import com.axjy.scheduler.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service("schedulerSeatService")
public class SchedulerSeatServiceImpl implements SchedulerSeatService {

    @Resource
    private SchedulerSeatPriceMapper schedulerSeatPriceMapper;


    @Resource
    private SchedulerSeatMapper schedulerSeatMapper;

    @Override
    public SeatArrayVo queryOriginalCinemaSeatArray(Integer scheduleId) {
        QueryWrapper<TpSchedulerSeat> queryWrapper = Wrappers.query();
        queryWrapper.eq("schedule_id",scheduleId);
        queryWrapper.orderByAsc("sort");
        List<TpSchedulerSeat> seatList = schedulerSeatMapper.selectList(queryWrapper);
        List<String> seatArray = new ArrayList<>();
        Integer max = 1;
        if (seatList.size()>0) {
            max = seatList.stream().max((a,b) -> a.getX()-b.getX())
                    .get().getX();
        }
        for (int x=1;x<=max;x++){
            StringBuffer sbf = new StringBuffer();
            for (TpSchedulerSeat seat:seatList){
                if(seat.getX()==x){
                    switch (seat.getAreaLevel()){
                        case 0:
                            sbf.append("_");
                            break;
                        case 1:
                            sbf.append("a");
                        case 2:
                            sbf.append("b");
                        case 3:
                            sbf.append("c");
                    }
                }
            }
            seatArray.add(sbf.toString());
        }
        SeatArrayVo vo = new SeatArrayVo();
        vo.setSeatArray(seatArray);
        return vo;
    }

    @Override
    public CinemaSeatVo queryCinemaSeatArrayByScheduleId(Integer scheduleId, Integer cinemaId) {
        CinemaSeatVo   vo = new CinemaSeatVo();
        vo.setScheduleId(scheduleId);
        vo.setCinemaId(cinemaId);
        QueryWrapper<TpSchedulerSeatPrice> queryWrapper = Wrappers.query();
        queryWrapper.eq("schedule_id",scheduleId);
        List<TpSchedulerSeatPrice> seatPriceList = schedulerSeatPriceMapper.selectList(queryWrapper);
        List<SeatPriceListVo> seatPriceListVos =   seatPriceList.stream()
                .map(tpSchedulerSeatPrice -> {
                    SeatPriceListVo seatPriceListVo = new SeatPriceListVo();
                    char ch =(char) ('A'-1+tpSchedulerSeatPrice.getAreaLevel());
                    seatPriceListVo.setAreaLevelName(ch+"");
                    seatPriceListVo.setPrice(tpSchedulerSeatPrice.getPrice());
                    return seatPriceListVo;
                }).collect(Collectors.toList());
        vo.setSeatPriceList(seatPriceListVos);

        QueryWrapper<TpSchedulerSeat> wrapper1 = Wrappers.query();
        wrapper1.eq("schedule_id",scheduleId);
        List<TpSchedulerSeat> schedulerSeatList = schedulerSeatMapper.selectList(wrapper1);
        List<SeatInfoListVo> seatInfoListVos =
                schedulerSeatList.parallelStream()
                .map(tpSchedulerSeat -> {
                    SeatInfoListVo seatInfoListVo = new SeatInfoListVo();
                    BeanUtils.copyProperties(tpSchedulerSeat,seatInfoListVo);
                    seatInfoListVo.setCinemaId(cinemaId);
                    QueryWrapper<TpSchedulerSeatPrice> wrapper2 = Wrappers.query();
                    wrapper2.eq("schedule_id",scheduleId);
                    wrapper2.eq("area_level",tpSchedulerSeat.getAreaLevel());
                    TpSchedulerSeatPrice tpSchedulerSeatPrice = schedulerSeatPriceMapper.selectOne(wrapper2);
                    if (tpSchedulerSeatPrice != null){
                        seatInfoListVo.setPrice(tpSchedulerSeatPrice.getPrice());
                    }
                    return seatInfoListVo;
                }).collect(Collectors.toList());
        vo.setSeatInfoList(seatInfoListVos);
        return vo;
    }
}